<template>
	<view class="container" v-show="isshow">
		<view class="card">
			<view class="title">
				<text>注册绑定小区/单位</text>
			</view>
			<view class="subtitle">
				<text>一个账号只能绑定一个小区/单位，绑定后自动成为小区/单位管理员，提供二维码给其他进出人员扫描并登记。所有人员第一次登录时需要完善个人信息，以后可自动登录。</text>
			</view>
		</view>
		<view class="section">
			<input @click="showCityPicker('from_address')" disabled="disabled" placeholder="请输入地址" :value="from_address.addressStr" />
		</view>
		<view class="section">
			<input v-model="from_address.fullName" placeholder="小区/单位名称" />
		</view>
		<button class="login-btn" type="primary" @click="login">注册绑定</button>

		<mpvue-city-picker ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onCityConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				from_address: {
					addressStr: '',
					street: ''
				},
				cityPickerValueDefault: [11, 0, 8],
				isshow: false
			};
		},
		onLoad(params) {
		},
		onShow() {
			let that = this;

			let u = uni.getStorageSync("userinfo")
			// 没有手机号，完善信息
			// 不是管理员的，显示注册页面
			// 没有小区的，显示注册页面
			// 已经绑定小区的，进入首页
			if (u.phone) {
				if (u.type != 'admin' || !u.oid) {
					that.isshow = true;
				} else {
					uni.switchTab({
						url: '/pages/tabbar/member'
					})
				}
			} else {
				uni.navigateTo({
					url: '/pages/userreg/userreg?f=a'
				})
			}
		},
		methods: {
			showCityPicker(segment) {
				this.tempPickerSegment = segment
				this.$refs.mpvueCityPicker.show();
			},
			onCityConfirm(e) {
				let addressParam = this[this.tempPickerSegment]
				addressParam.province = e.value[0]
				addressParam.city = e.value[1]
				addressParam.district = e.value[2]
				addressParam.addressStr = e.label
			},
			login() {
				if (this.from_address.fullName.trim() == "") {
					uni.showToast({
						title: '请输入小区/单位名称！',
						icon: 'none',
						duration: 2000
					});
					return false;
				}
				if (this.from_address.addressStr.trim() == "") {
					uni.showToast({
						title: '请选择所在地区',
						icon: 'none',
						duration: 2000
					});
					return false;
				}
				let that = this;
				this.$sendhttp("/Weixin/createAndBindOrg.dhtml", {
					"fullName": this.from_address.fullName,
					"address": this.from_address.addressStr
				}, function(data) {
					let u = uni.getStorageSync("userinfo");
					u.oname = that.from_address.fullName;
					u.oid = data.oid;
					uni.setStorageSync("userinfo", u);
					uni.switchTab({
						url: '/pages/tabbar/member'
					})
				});
			}
		}
	}
</script>

<style scoped>
	@import "../../static/css/global.css";

	.container {
		padding: 30px;
	}

	.section {
		margin: 20px;
		padding: 10px 0px;
		border-bottom: 2upx solid #eee;
	}

	.login-btn {
		margin: 30px 20px 0px 20px;
	}
</style>
